package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/findById")
public class ProductFindByIdServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //取值，取得前端页面传递过来的数据
        String id = req.getParameter("id");
//封装数据


        ProductService productService = new ProductServiceImpl();
//        取得返回结果,如果参数大于两个，且类型一致，都是属于同一个对象--和数据库的表一一对应
        ProductModel model = productService.findById(Tool.stringToInt(id));
//返回数据json对象的格式,如果要用json格式，我们需要加入第三方jar包
        resp.getWriter().println();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model", model);
        resp.getWriter().println(jsonObject);
    }
}
